<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $event;
  var $songs;
  var $setlistSongs;
  var $setlistIds;
  var $notes;

  function PageController() {
	parent::__construct();

	$this->title="Setlist";
	$this->section="events";

	$action = $this->getParameter("action");
	$this->event = new Event();
	$this->songs = SongLogic::findAll("title");

	switch (strtoupper($action))
	{
		case "ADD TO SETLIST" :
			  $songIds = $this->getParameter("newSongs");
			  $eventId = $this->getParameter("event_id");

			  EventLogic::addToSetlist($eventId, $songIds);
			  $this->event = EventLogic::load($eventId);

			  break;
		case "REMOVE" :
			  $setlistId = $this->getParameter("id");
			  $this->event->id = $this->getParameter("event_id");

			  EventLogic::removeFromSetlist($this->event->id, $setlistId);
			  $this->event = EventLogic::load($this->event->id);
			  break;
		case "SAVE" :
			  $this->parseForm();
			  if (!$this->validate()) return;

			 $order = 1;
			 $setlist = array();
			 foreach (split("[\|]", $this->setlistIds) as $setlistId)
			 {
				$setlistItemInfo = split("[\-]", $setlistId);
			 	array_push($setlist, new EventToSongMap($setlistItemInfo[0], $this->event->id, $setlistItemInfo[1], $order, $this->notes[$setlistItemInfo[0]]));
			 	$order++;
			 }
			 EventLogic::saveSetlist($setlist);

			  $this->addMessage("Saved");
			  break;

		case "LOAD" :
			  $id = $this->getParameter("id");
	  		  $this->event = EventLogic::load($id);

	  		  $this->addMessage("Loaded $id");
	  		  break;
	  	default:

	}

	if ($this->event->id != 0)
	{
		$this->setlistSongs = EventLogic::loadSetlist($this->event->id);
	    $seperator = "";
	    foreach ($this->setlistSongs as $setlistSong)
		{
		  	 $this->setlistIds .= $seperator . $setlistSong->song->id;
		  	 $seperator = "|";
		}
	}
  }

  function parseForm() {
	$this->event->id = $this->getParameter("event_id");
	$this->setlistIds = $this->getParameter("setlistIds");
	$this->notes = $this->getParameter("notes");

  }

  function validate() {
	return (sizeof($this->errs) == 0);
  }

}



?>
